package io.gitee.hejiang;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class SwaggerSqlSPAController {
	@Controller
	@ConditionalOnProperty(prefix = "spring.mvc", name = "spa-base-path")
	@ConditionalOnExpression("!'${spring.mvc.spa-base-path}'.equals('/')")
	public class CommonSPAController {
		@Value("${spring.mvc.spa-base-path}")
		String spaBasePath;

		@GetMapping(value = "/")
		public String root() {
			return "redirect:" + spaBasePath;
		}

		// 将所有无后缀的路径(/, /abc, /abc/, /abc/def)的 GET 请求全部映射到 /*
		@GetMapping(value = { "**/{:[^.]*}" })
		public String index(HttpServletRequest request) {
			String uri = request.getRequestURI();
			int end = uri.indexOf('/', 1);
			return end > 0 ? uri.substring(0, end) : uri;
		}
	}

	@Controller
	@ConditionalOnProperty(prefix = "spring.mvc", name = "spa-base-path", havingValue = "/")
	public class SpecialSPAController {
		// 将所有无后缀的路径(/, /abc, /abc/, /abc/def)的 GET 请求全部映射到 /*
		@GetMapping(value = { "**/{:[^.]*}" })
		public String index(HttpServletRequest request) {
			String uri = request.getRequestURI();
			int end = uri.indexOf('/', 1);
			return end > 0 ? uri.substring(0, end) : uri;
		}
	}
}
